package NWish.properties.materials
{
	import NWish.Interface.ILogger;
	
	import NWish.json.OrderDict;
	import NWish.properties.Properties;
	
	import away3d.containers.Scene3D;
	import away3d.materials.SkyBoxMaterial;

	public class SkyBoxMaterialProperties extends MaterialBaseProperties
	{
		public function SkyBoxMaterialProperties(parent,materialPropertiesTypes)
		{
			super(parent,materialPropertiesTypes);
		}
		
		
		override public function propertiesToDict(obj,dict:OrderDict,read_only = false,saving = false)
		{
			super.propertiesToDict(obj,dict,read_only,saving);
			
			var mat:SkyBoxMaterial = obj as SkyBoxMaterial;
			
			
			if(read_only == false)
			{
				if(mat.extra == null)
					throw new Error();
				
				dict.push('positiveX',mat.extra.positiveX);
				dict.push('negativeX',mat.extra.negativeX);
				dict.push('positiveY',mat.extra.positiveY);
				dict.push('negativeY',mat.extra.negativeY);
				dict.push('positiveZ',mat.extra.positiveZ);
				dict.push('negativeZ',mat.extra.negativeZ);
				
				
				if(saving == false)
				{
					var textures = fileSystem.getFileNames('.jpg','.png');
					dict.push(Properties.readMark + 'textuers', textures);
				}
			}
			else
			{
				dict.push(Properties.readMark + 'size', mat.cubeMap.size);
			}
		}
		
		override public function dictToProperties(dict,obj,scene:Scene3D,isloading)
		{
			super.dictToProperties(dict,obj,scene,isloading);
			
			var mat:SkyBoxMaterial = obj as SkyBoxMaterial;
			//
			loadfile(mat,dict['positiveX'],'positiveX');
			loadfile(mat,dict['negativeX'],'negativeX');
			loadfile(mat,dict['positiveY'],'positiveY');
			loadfile(mat,dict['negativeY'],'negativeY');
			loadfile(mat,dict['positiveZ'],'positiveZ');
			loadfile(mat,dict['negativeZ'],'negativeZ');
		}
		
		
		private function loadfile(mat:SkyBoxMaterial,name,key)
		{
			if(name == null)
			{
			}
			else if (name == '')
			{
				//mat.extra[key] = '';
				//mat.cubeMap[key] = null;
				ILogger.log('please select a texture!');
			}
			else if(_app)
			{
				var dm = fileSystem.getResource(name);
				
				if(dm)
				{
					mat.extra[key] = name;
					
					mat.cubeMap[key] = dm;
					
					return;
				}
			}
		}
		
		//
		override public function toString()
		{
			return 'SkyBoxMaterial';
		}
		override public function get type()
		{
			return SkyBoxMaterial;
		}
	}
}





